source('00_util_scripts/mod_bplot.R')
source('00_util_scripts/mod_bulk.R')

proj.nm <- 'mission/SLE_TRPM2_MfMo/'

gene_of_int <- c('TRPM2','HMGB2','CCR2','CD14','TNF')

# GSE236713 ----------
szak23 <- pluck_geo('GSE236713')

szak23_goi <- szak23 |>
  fData() |>
  as_tibble() |>
  filter(GENE_SYMBOL %in% gene_of_int)

glimpse(szak23)

szak_sepsis <- szak23[,szak23$disease.ch1 != 'SIRS']

szak_sepsis$disease.ch1 |> table()

szak_sepsis$disease.2.ch1 |> table()

szak_sepsis$group <- szak_sepsis$disease.ch1 |>
  fct_relevel('Sepsis')

szak_res <- szak_sepsis |>
  geo_limma(gene_col = 'GENE_SYMBOL')

szak_res |>
  filter(gene %in% gene_of_int) |>
  mutate(dataset = 'Sepsis_GSE236713') |>
  write_source_csv('Sepsis_GSE236713_array')

## outcome ------------
szak_outcome <- szak_sepsis[,szak_sepsis$group == 'Sepsis']

szak_outcome$died.survived.ch1 |> table()

szak_outcome$group <- szak_outcome$died.survived.ch1

szak_surv_res <- szak_outcome |>
  geo_limma(gene_col = 'GENE_SYMBOL')

szak_surv_res |>
  filter(gene %in% gene_of_int)

szak_outcome['A_24_P27977',] |>
  makeSummarizedExperimentFromExpressionSet() |>
  ggplot(aes(group, exprs, fill = group)) +
  geom_boxplot() +
  geom_jitter(height = 0, width = .1) +
  labs(title = 'TRPM2 expression in sepsis patient PBMC',
       subtitle = 'GSE236713 (59 Died vs 265 Survived)', y = 'Expression') +
  theme_pubr() +
  stat_compare_means(comparisons = list(c('Died','Survived')),
                     label = 'p.signif')

# GSE137340 ------------
mukhop22 <- pluck_geo('GSE137340')

mukhop22 |>
  pData() |>
  glimpse()

mukhop22$diagnosis.ch1 |> table()

mukhop22 <- mukhop22[,mukhop22$diagnosis.ch1 != 'NA']

mukhop22$group <- ifelse(mukhop22$diagnosis.ch1 == 'Healthy', 'HC', 'Sepsis') |>
  fct_relevel('Sepsis')

mukhop22$group |> table()

mukhop22 |>
  fData() |>
  glimpse()

mukhop_res <- mukhop22 |>
  geo_limma()

mukhop_res |>
  filter(gene %in% gene_of_int) |>
  summarise(adj.P.Val = median(adj.P.Val), logFC = median(logFC), .by = gene) |>
  mutate(dataset = 'Sepsis_GSE137340') |>
  write_source_csv('Sepsis_GSE137340_array')

# GSE69063 ---------
bosco15 <- pluck_geo('GSE69063')

bosco15 |>
  fData() |>
  glimpse()

bosco_gene <- bosco15 |>
  fData()

entrez2symbol <- bosco_gene$ENTREZ_GENE_ID |>
  clusterProfiler::bitr(fromType = 'ENTREZID', toType = 'SYMBOL',
                        OrgDb = 'org.Hs.eg.db', drop = F)

fData(bosco15) <- bosco_gene |>
  mutate(ENTREZID = as.character(ENTREZ_GENE_ID)) |>
  left_join(entrez2symbol)

bosco15 |>
  pData() |>
  glimpse()

bosco15$disease.status.ch1 |> table()

bosco_sepsis <-
  bosco15[,bosco15$disease.status.ch1 %in% c('Healthy control','Sepsis')]

bosco_sepsis$group <- bosco_sepsis$disease.status.ch1 |>
  make.names() |>
  fct_relevel('Sepsis')

bosco_sepsis$group |> table()

bosco_res <- bosco_sepsis |>
  geo_limma(gene_col = 'SYMBOL')

bosco_res |>
  filter(gene %in% gene_of_int) |>
  mutate(dataset = 'Sepsis_GSE69063') |>
  write_source_csv('Sepsis_GSE69063_array')

